package alert

import (
	"fmt"
	"net/smtp"

	"monitor.ilin.cc/config"
)

type EmailNotifier struct {
	config config.AlertChannel
}

func NewEmailNotifier(cfg config.AlertChannel) *EmailNotifier {
	return &EmailNotifier{
		config: cfg,
	}
}

func (n *EmailNotifier) Send(message string) error {
	auth := smtp.PlainAuth("", n.config.Username, n.config.Password, n.config.SMTPHost)

	to := []string{n.config.Username} // 可以从配置中读取收件人列表
	msg := []byte(fmt.Sprintf("To: %s\r\n"+
		"Subject: OKX Market Alert\r\n"+
		"\r\n"+
		"%s\r\n", n.config.Username, message))

	addr := fmt.Sprintf("%s:%d", n.config.SMTPHost, n.config.SMTPPort)
	if err := smtp.SendMail(addr, auth, n.config.Username, to, msg); err != nil {
		return fmt.Errorf("send email failed: %v", err)
	}

	return nil
}
